package cn.ds.assets.module.assets.controller.admin.dataQualityRuleSet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.dataQualityRuleSet.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleSet.BaseDataQualityRuleSetDO;
import cn.ds.assets.module.assets.convert.dataQualityRuleSet.BaseDataQualityRuleSetConvert;
import cn.ds.assets.module.assets.service.dataQualityRuleSet.BaseDataQualityRuleSetService;

//@Tag(name = "管理后台 - 数据质量规则设置")
//@RestController
//@RequestMapping("/assets/base-data-quality-rule-set")
@Validated
public class BaseDataQualityRuleSetController {

    @Resource
    private BaseDataQualityRuleSetService baseDataQualityRuleSetService;

    @PostMapping("/create")
    @Operation(summary = "创建数据质量规则设置")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:create')")
    public CommonResult<Long> createBaseDataQualityRuleSet(@Valid @RequestBody BaseDataQualityRuleSetCreateReqVO createReqVO) {
        return success(baseDataQualityRuleSetService.createBaseDataQualityRuleSet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据质量规则设置")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:update')")
    public CommonResult<Boolean> updateBaseDataQualityRuleSet(@Valid @RequestBody BaseDataQualityRuleSetUpdateReqVO updateReqVO) {
        baseDataQualityRuleSetService.updateBaseDataQualityRuleSet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据质量规则设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:delete')")
    public CommonResult<Boolean> deleteBaseDataQualityRuleSet(@RequestParam("id") Long id) {
        baseDataQualityRuleSetService.deleteBaseDataQualityRuleSet(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据质量规则设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:query')")
    public CommonResult<BaseDataQualityRuleSetRespVO> getBaseDataQualityRuleSet(@RequestParam("id") Long id) {
        BaseDataQualityRuleSetDO baseDataQualityRuleSet = baseDataQualityRuleSetService.getBaseDataQualityRuleSet(id);
        return success(BaseDataQualityRuleSetConvert.INSTANCE.convert(baseDataQualityRuleSet));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据质量规则设置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:query')")
    public CommonResult<List<BaseDataQualityRuleSetRespVO>> getBaseDataQualityRuleSetList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseDataQualityRuleSetDO> list = baseDataQualityRuleSetService.getBaseDataQualityRuleSetList(ids);
        return success(BaseDataQualityRuleSetConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据质量规则设置分页")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:query')")
    public CommonResult<PageResult<BaseDataQualityRuleSetRespVO>> getBaseDataQualityRuleSetPage(@Valid BaseDataQualityRuleSetPageReqVO pageVO) {
        PageResult<BaseDataQualityRuleSetDO> pageResult = baseDataQualityRuleSetService.getBaseDataQualityRuleSetPage(pageVO);
        return success(BaseDataQualityRuleSetConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据质量规则设置 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-set:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseDataQualityRuleSetExcel(@Valid BaseDataQualityRuleSetExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseDataQualityRuleSetDO> list = baseDataQualityRuleSetService.getBaseDataQualityRuleSetList(exportReqVO);
        // 导出 Excel
        List<BaseDataQualityRuleSetExcelVO> datas = BaseDataQualityRuleSetConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据质量规则设置.xls", "数据", BaseDataQualityRuleSetExcelVO.class, datas);
    }

}
